<?php

$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
$typelistdv = '';

if ($id > 0) {
    $inforow = Db::getOneRow("*", 'article', 'id=' . $id);
    $typerows = Db::getRows('*', 'type', 'menu=4');
    foreach ($typerows as $typerow) {
        $selected = $inforow['type'] == $typerow['id'] ? 'selected="selected"' : '';
        $typelistdv .= '<option '.$selected.' value="' . $typerow['id'] . '">' . $typerow['name'] . '</option>';
    }
} else {
    Js::gourl(baseUrl() . '/dichvuList');
    exit;
}
if (Http::isPost()) {
    $type = $_POST['typelist'];
    $title = Form::input('title', 'ne');
    $intro = Form::input('intro');
    $content = Form::input('content');
    $title2 = Form::input('title2', 'ne');
    $intro2 = Form::input('intro2');
    $content2 = Form::input('content2');
    $posteddate = date('H:i d-m-Y');
    $img = '';
    if (Form::isValid()) {
        if ($id > 0) {
            $curpath = $_SERVER['DOCUMENT_ROOT'] . str_replace('/admin', '/upload/', baseUrl());

            if ($_FILES["file"]["error"] > 0 && $_FILES["file"]["error"] != '4') {
                echo "Có lổi: " . $_FILES["file"]["error"] . "<br />";
            } else if ($_FILES["file"]["error"] == '4') {
                $eval = eval(Db::update('article', "id=$id", array_merge(array('id', 'img','nation','level'))));
                if (mysql_affected_rows() > 0) {
                    Js::alert(fl('success_update'));
                    Js::GoUrl(baseUrl() . '/dichvuList');
                    exit();
                }
            } else {
                $upload = Http::UploadImage('file', str_replace('//', '/', $curpath), '', $img);
                if ($upload === '') {
                    $eval = eval(Db::update('article', "id=$id", array_merge(array('id','nation','level'))));
                    if (mysql_affected_rows() > 0) {
                        Js::alert(fl('success_update'));
                        Js::GoUrl(baseUrl() . '/dichvuList');
                        exit();
                    }
                } else {
                    Js::alert($upload);
                }
            }
        }
    }
}
?>
